
Ext.define('JS.controller.menu.MainMenuController', {
	extend   : 'Ext.app.Controller',
	alias    : 'widget.mainmenucontroller',
	views    : ['menu.MainMenuTree', 'info.DeptGrid'],
	stores   : ['MainMenuTreeStore', 'MainInfoGridStore'],
	models   : ['MainMenuTreeModel', 'ModelFactory'],
	
	refs: [{//相当于一个映射,这样就可以在控制层方便的通过geter取得相应的对象了
        ref: 'deptgrid', 
        selector: 'deptgrid'
    }],
	mainGridList : new Ext.util.MixedCollection(),//缓存节点菜单对应的grid
	columns      : new Ext.util.MixedCollection(),//缓存grid的列模式
	storeList    : new Ext.util.MixedCollection(),//缓存store
	
	init     : function() {
 		this.control({
 			'mainmenutree' : {
 				itemclick : function(tree, record, item, e, opti) {//点击树节点添加详细信息grid
 					var id = 'maintab' + record.getId();//设置grid的id
 					
 					var mainTab = tree.ownerCt.ownerCt.previousSibling();//获取位置为center的tabpanel
 					
 					if(!this.mainGridList.containsKey(id)) {//查看缓存中是否存在已生成的grid
 						this.mainGridList.add(id, this.getGridByMenu(record));
 					}
 					
 					var grid = this.mainGridList.getByKey(id);
 					
 					if(mainTab.queryById(id) == null) {//如果tabpanel中已经有对应id的grid则不用添加
 						mainTab.add(grid);
 					}
 					
 					grid.show();
 				}
 			}
 		});
 	},
 	getGridByMenu : function(record) {//点击树形菜单获取到对应的grid，record对应树形节点
	 	var id = 'maintab' + record.getId();//设置grid的id
	 	var title = record.get('text');
 		
 		var grid = Ext.create('JS.view.info.DeptGrid', {
 			id     : id,
 			title  : title,
 			store  : this.getStoreByMenu(record),
 			columns: this.getColumnsByMenu(record)//dataObj//[{"text":"部门编号","dataIndex":"code","width":100},{"text":"部门名称","dataIndex":"name","width":100},{"text":"部门经理","dataIndex":"manager","width":100},{"text":"职能介绍","dataIndex":"info","width":100}]
 		});
 		return grid;
 	},
 	getColumnsByMenu : function(record) {//点击树形菜单，获取grid需要的column，record对应树形节点
 		var id = 'maintab' + record.getId();//设置grid的id
		var code = record.get('code');
		
 		var dataObj = [];
 		if(this.columns.containsKey(id)) {
 			dataObj = this.columns.get(id);
 		} else {
 			Ext.Ajax.request({
	 			url     : '../../info/gridcolumn',
	 			params  : {code : code},
	 			async   : false,
	 			timeout : 4000,
	 			success : function(response) {
	 				var data = response.responseText;
	 				dataObj = eval('(' + data + ')');
	 			}
	 		});
	 		this.columns.add(id, dataObj);
 		}
 		return dataObj;
 	},
 	getStoreByMenu : function(record) {//获取grid需要的store
 		var id = 'maintab' + record.getId();//设置grid的id
 		var code = record.get('code');
 		
 		var storeName;
 		if(this.storeList.containsKey(id)) {
 			storeName = this.storeList.get(id);
 		} else {
 			Ext.Ajax.request({
 				url     : '../../info/gridstore',
 				params  : {code : code},
 				async   : false,
 				timeout : 4000,
 				success : function(response) {
 					var data = response.responseText;
 					storeName = data;
 				}
 			});
 			this.storeList.add(id, storeName);
 		}
 		return storeName;
 	}
});
